import random
import arcade

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
SPRITE_SCALE = 0.8  # 图形缩放系数，用于调整人物的大小
COIN_NUM = 50  # 金币数量

class MyGame(arcade.Window):

    def __init__(self):
        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, "Sprite Example")

        # 存储玩家角色与金币的列表变量
        self.player_list = None
        self.coin_list = None

        # 创建初始角色信息
        self.player = None
        self.score = 0

        # 隐藏鼠标指针
        self.set_mouse_visible(False)

        arcade.set_background_color(arcade.color.AMAZON)

    def setup(self):
        # 创建arcade SpriteList对象，SpriteList对象能帮助我们管理多个Sprite角色
        self.player_list = arcade.SpriteList()
        self.coin_list = arcade.SpriteList()

        # 分数
        self.score = 0

        # 初始化用户角色
        self.player = arcade.Sprite(":resources:images/animated_characters/female_adventurer/femaleAdventurer_idle.png",
                                    scale=SPRITE_SCALE)
        self.player.center_x = 250
        self.player.center_y = 250
        self.player_list.append(self.player)

        # 批量生成金币
        for i in range(COIN_NUM):
            coin = arcade.Sprite(":resources:images/items/gold_1.png", 0.5)

            coin.center_x = random.randrange(SCREEN_WIDTH)
            coin.center_y = random.randrange(SCREEN_HEIGHT)

            self.coin_list.append(coin)

    def on_draw(self):
        arcade.start_render()

        # 绘制Sprite列表
        self.coin_list.draw()
        self.player_list.draw()

        # 绘制分数
        score_text = "Score:" + str(self.score)
        arcade.draw_text(score_text, 10, 20, arcade.color.WHITE, 14)

    def on_mouse_motion(self, x, y, dx, dy):
        self.player.center_x = x
        self.player.center_y = y

    def update(self, delta_time):
        collected_coins = arcade.check_for_collision_with_list(self.player,
                                                               self.coin_list)

        for coin in collected_coins:
            coin.remove_from_sprite_lists()
            self.score += 1

def main():
    window = MyGame()
    window.setup()
    arcade.run()

if __name__ == "__main__":
    main()